module NovelsHelper
  def latest_fragments
    fragments = get_latest_fragments
    html = "<div>"
    return html << "<h5>还没有任何续篇...</h5></div>" if fragments.nil? or fragments.empty?
    html << "<h5>最新续篇</h5>"
    fragments.each do |fragment|
      html<<"<p>"<<time_format(fragment.created_at) <<link_to_user_show(fragment.user)<<"续   "<<truncate(fragment.content, 60)<<"给"<<link_to_novel_show(fragment.novel)<<"</p>"
    end
    html<<"</div>"
    html
  end

  def latest_votes
    vote_lables={Vote::FRAGMENT_VOTE_GOOD=>"精彩", Vote::FRAGMENT_VOTE_BAD=>"一般", Vote::FRAGMENT_VOTE_MORE_BAD=>"糟糕"}
    votes = get_latest_votes
    html = "<div>"
    return html << "<h5>没有任何读者投票</h5></div>" if votes.nil? or votes.empty?
    html << "<h5>读者看看</h5>"
    votes.each do |vote|
      html<<"<p>"<<link_to_user_show(vote.user)<<"投<strong>#{vote_lables[vote.level]}</strong>给片段#{link_to_fragment_show(vote.fragment)}"<<"</p>"
    end
    html<<"</div>"
    html
  end

  def latest_comments
    comments = get_latest_comments
    html = "<div>"
    return html << "<h5>没有任何评论...</h5></div>" if comments.nil? or comments.empty?
    html << "<h5>最新续篇</h5>"
    comments.each do |comment|
      html<<"<p>"<<link_to_user_show(comment.user)<<"说："<<truncate(comment.content, 100)<<"</p>"
    end
    html<<"</div>"
    html
  end

  def compose_novel_show_sidebar
	  html = "<div>"
	  chapters = @novel.chapters
	  if chapters and !chapters.empty?
		html<<"<h4>Already merged:</h4>"
		chapters.each do |chapter|
		  html<<"<h3>#{chapter.sequence}. #{chapter.title}</h3>"
		  html<<"<p>#{chapter.merged_fragment ?  chapter.merged_fragment.content.truncate(120) : " writing... " }</p>"
		end
	  else
		html << "<h4>Have no chapters.</h4></div>"
	  end
	  html<<"</div>"
	  html
  end

  def label_for_linkrule  linkrue
    case linkrue
    when Novel::LINK_RULE_1
      Novel::LINK_RULE_1_DESC
    when Novel::LINK_RULE_2
      Novel::LINK_RULE_2_DESC
    when Novel::LINK_RULE_3
      Novel::LINK_RULE_3_DESC
    end
  end


  private

  def link_to_open_show novel
    return if novel.is_open
    link = link_to "打开文章续写？", :controller=>"novel", :action=>"open", :id=>@novel
    link if novel.user_id == session[:user_id]
  end

  def link_to_over_show novel
    return if novel.is_over
    link = link_to "结束文章续写？", :controller=>"novel", :action=>"over", :id=>@novel
    link if novel.user_id == session[:user_id]
  end

  def link_to_user_show user
    return "<strong>佚名</strong>" unless user
    link_to user.name, :controller=>"user", :action=>"show", :id=>user
  end

  def link_to_novel_show novel
    link_to novel.title, :controller=>"novel", :action=>"show", :id=>novel
  end

  def link_to_fragment_show fragment
    link_to truncate(fragment.content, 12), :controller=>"fragment", :action=>"show", :id=>fragment
  end

  def link_to_fragment_show_with_label fragment, label
    link_to label, :controller=>"fragment", :action=>"show", :id=>fragment
  end

  def get_latest_novels
    Novel.find :all, :limit=>5, :order=>"created_at DESC"
  end

  def get_latest_votes
    Vote.find :all, :limit=>5, :order=>"created_at DESC"
  end

  def get_latest_fragments
    Fragment.find :all, :limit=>5, :order=>"created_at DESC"
  end

  def get_latest_comments
    Comment.find :all, :limit=>5, :order=>"created_at DESC"
  end
  
  def rss_link novel
	url_for :action=>'feed',:id=>novel,:format=>"rss",:only_path=>false
	"http://localhost:3000/novel/feed/12.rss"
  end
end
